package com.fatdetection.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fatdetection.demo.annotation.AuthAdmin;
import com.fatdetection.demo.annotation.AuthOperator;
import com.fatdetection.demo.bean.LayuiTableRst;
import com.fatdetection.demo.bean.Project;
import com.fatdetection.demo.service.ProjectService;
import com.fatdetection.demo.util.RstMsgUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
@AuthAdmin
@AuthOperator
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping("/searchProjsByAdminName")
    public Object searchProjsByAdminName(String adminName, String info){
        List<Project> projects = projectService.searchProjsByAdminName(adminName, info);
        if(projects == null){
            return RstMsgUtils.error("暂无记录");
        }else{
            Map<String, Object> map = new HashMap<>();
            map.put("count", projects.size());
            map.put("data", projects);
            return map;
        }
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", required = true),
            @ApiImplicitParam(name = "size", value = "每页的条目数", required = true),
            @ApiImplicitParam(name = "searchKey", value = "搜索的键（对应数据表字段，eg: username）", required = false),
            @ApiImplicitParam(name = "searchVal", value = "模糊搜索的值", required = false)
    })
    @RequestMapping(value = "/selectPage", method = RequestMethod.GET)
    public Object selectPage(Integer current
            , Integer size
            , String searchKey
            , String searchVal){
        Page<Project> page = projectService.selectAllPage(current, size, searchKey, searchVal);
        LayuiTableRst layuiTableRst = new LayuiTableRst();
        layuiTableRst.setCode(0);
        layuiTableRst.setCount((int) page.getTotal());
        layuiTableRst.setMsg("获取成功");
        layuiTableRst.setData(page.getRecords());
        return layuiTableRst;
    }
    @PostMapping("/add")
    public Object insertProject(Project project){
        int count = projectService.insertProject(project);
        if(count > 0){
            return RstMsgUtils.success( "添加成功", "");
        }else {
            return RstMsgUtils.error("添加失败");
        }
    }

    @PostMapping("/del")
    public Object delProject(int id){
        int count = projectService.delProject(id);
        if(count > 0){
            return RstMsgUtils.success("删除成功", "");
        }else {
            return RstMsgUtils.error("删除失败");
        }
    }

    @PostMapping("/update")
    public Object updateProject(Project project){
        int count = projectService.updateProject(project);
        if(count > 0){
            return RstMsgUtils.success("修改成功", "");
        }else {
            return RstMsgUtils.error("修改失败");
        }
    }

    @GetMapping("/findAll")
    public List<Project> getAllProject(){
        List<Project> list = projectService.findAll();
        return list;
    }

    @GetMapping("/findByAdmin/{name}")
    public List<Project> getProjectByAdmin(@PathVariable String name){
        List<Project> list = projectService.findProjByAdmin(name);
        return list;
    }

    @GetMapping("/findById/{id}")
    public Project getProjectById(@PathVariable Integer id){
        Project project = projectService.findById(id);
        return project;
    }

    @GetMapping("/findByName/{name}")
    public Project getProjectByName(@PathVariable String name){
        Project project = projectService.findProjByName(name);
        return project;
    }

    @GetMapping("/findByMeas")
    public List<Project> getProjectByMeas(){
        List<Project> list = projectService.findMeasProj(1);
        return list;
    }

    @GetMapping("/findByData")
    public List<Project> getProjectByData(){
        List<Project> list = projectService.findDataProj(1);
        return list;
    }
}
